package com.cby.leetcode.二分查找.LC34;

/**
 * ClassName: LC34
 * Package: com.cby.leetcode.二分查找.LC34
 * Description:
 *
 * @Author dhu-bion
 * @Create 2025/9/11 16:01
 * @Version 1.0
 */
public class LC34 {
    public static void main(String[] args){

    }

    /**
     * 二分查找：lowBound
     *
     * 递增整数数组二分查找符号分类转换（本题找=，等价于lowBound找>=和<=）
     * >=:x
     * >:转为>=x+1
     * <=:转为>=x+1左边一位
     * <:转为>=x左边一位数
     */

    static class Solution{
        public int[] searchRange(int nums[],int target){
            //问题转换为找>=和<=
            int res = this.findLowerBound(nums, target);
            //数组中所有数都小于target或者出循环仍然没有找到
            if(res == nums.length || nums[res] != target)
                return new int[]{-1,-1};
            return new int[]{res,this.findLowerBound(nums,target + 1) - 1};
        }


        //寻找下限
        private int findLowerBound(int nums[],int target){
            int left = 0;
            int right = nums.length - 1;  //截取小数前
            int mid;

            while(left <= right){
                mid = left + (right - left) / 2;
                if(nums[mid] >= target){
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }

            //找到：最左边的target/未找到：等于数组长度
            return left;
        }
    }
}
